package com.briup.myblog.web.controller;


import com.briup.myblog.aop.Logging;
import com.briup.myblog.bean.basic.BaseUser;
import com.briup.myblog.dao.ext.UserMapper;
import com.briup.myblog.exception.CustomerException;
import com.briup.myblog.service.IBaseUserService;
import com.briup.myblog.utils.Result;
import com.briup.myblog.utils.ResultUtil;
import com.briup.myblog.web.vm.UserUpdateVM;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "用户模块")
@RestController
@RequestMapping("/auth/user")
public class UserController {
    @Autowired
    private IBaseUserService service;
    @ApiOperation("根据id删除用户信息")
    @ApiImplicitParam(name = "userId",value = "用户编号",required = true)
    @Logging
    @DeleteMapping("/delete")
    public Result deleteUserById(Integer id){
                 service.deleteUserById(id);
                 return ResultUtil.success("删除成功");

    }
    @ApiOperation("根据用户名和角色名分页查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role",value = "角色"),
            @ApiImplicitParam(name = "username",value = "用户名"),
            @ApiImplicitParam(name = "pageNum",value = "起始页",required = true),
            @ApiImplicitParam(name = "pageSize",value = "页长",required = true)
    })
    @Logging
    @GetMapping("/page/query")
    public Result findUserByPageHelper(Integer role,String username,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<BaseUser> pageInfo = service.findUserByPageHelper(role,username,pageNum,pageSize);
        return ResultUtil.success(pageInfo);
    }
    @ApiOperation("根据id修改用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户编号",required = true),
            @ApiImplicitParam(name = "telephone",value = "电话号码",required = true),
            @ApiImplicitParam(name = "realname",value = "真实姓名",required = true),
            @ApiImplicitParam(name = "gender",value = "性别",required = true),
            @ApiImplicitParam(name = "dob",value = "出生日期",required = true),
            @ApiImplicitParam(name = "email",value = "电子邮箱",required = true)
    })
    @Logging
    @PutMapping("/update")
    public Result updateUser(UserUpdateVM vm,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        BaseUser baseUser = service.currentUserInfo(token);
        BaseUser user = new BaseUser();
        user.setUserId(vm.getUserId());
        if(baseUser.getRole()==0){
                user.setTelephone(vm.getTelephone());
                user.setRealname(vm.getRealname());
                user.setGender(vm.getGender());
                user.setDob(vm.getDob());
                user.setEmail(vm.getEmail());
                service.updateUser(user);
                BeanUtils.copyProperties(vm,user);
                return ResultUtil.success("更新成功");
        }
        else {
            if(user.getUserId()!=baseUser.getUserId()){
                throw new CustomerException("当前登录用户为普通用户，只能对个人信息进行操作");
            }
            else {
                user.setTelephone(vm.getTelephone());
                user.setRealname(vm.getRealname());
                user.setGender(vm.getGender());
                user.setDob(vm.getDob());
                user.setEmail(vm.getEmail());
                service.updateUser(user);
                BeanUtils.copyProperties(vm,user);
                return ResultUtil.success("更新成功");
            }
        }



    }
    @ApiOperation("根据用户id修改用户头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户编号",required = true),
            @ApiImplicitParam(name = "icon",value = "头像",required = true)
    })
    @Logging
    @PutMapping("/updateUserIcon")
    public Result updateUserIconById(int id,String userIcon,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        BaseUser baseUser = service.currentUserInfo(token);
        if(baseUser.getRole()==0){
            service.updateUserIcon(id,userIcon);
            return ResultUtil.success("修改用户头像成功");
        }
        else {
            if(baseUser.getUserId()!=id){
                throw new CustomerException("当前登录用户为普通用户，只能对个人信息进行操作");
            }
            else {
                service.updateUserIcon(id,userIcon);
                return ResultUtil.success("修改用户头像成功");
            }
        }

    }
    /*@ApiOperation("获取当前登录用户的基本信息")
    @Logging
    @GetMapping("/info")
    public Result currentUserInfo(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        BaseUser user =  service.currentUserInfo(token);
        return ResultUtil.success(user);
    }*/



}
